// pages/cart/cart.js
import { Cart } from "./cart-model";
var cart = new Cart();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var cartData = cart.getCartDataFromLocal();
    var cal = this._calcTotalAccountAndCounts(cartData);
    var userArray = cart.checkUser();
    this.setData({
      cartData:cartData,
      selectedTypeCounts: cal.selectedTypeCounts,
      selectedCounts: cal.selectedCounts,
      account: cal.account,
      userArray:userArray,
    });
  },

   /**
   *计算总金额和选择的商品总数
   */
  _calcTotalAccountAndCounts: function (data) {
    var len = data.length;

    //选择商品总价格
    var account = 0;
    // 购买商品总个数
    var selectedCounts = 0;
    // 购买商品种类总数
    var selectedTypeCounts = 0;

    let multiple = 100;
    for (let i = 0; i < len; i++) {
      //避免 0.05 + 0.01 = 0.060 000 000 000 000 005 的问题，乘以 100 *100
      if (data[i].checkStatus) {
        account += data[i].counts * multiple * Number(data[i].price) * multiple;
        // 总价
        selectedCounts += data[i].counts;
        // 总数量
        selectedTypeCounts++;
      }
    }
    return {
      selectedCounts: selectedCounts,
      selectedTypeCounts: selectedTypeCounts,
      account: account / (multiple * multiple)
    }
  },
 //商品选择
  toggleSelect(event){
    var status = cart.getDataSet(event, 'status');
    var id = cart.getDataSet(event, 'id');
    var index = this._getProductIndexById(id);
    this.data.cartData[index].checkStatus = !status;
    this._resetCartData();
  },

   /*根据商品id得到 商品所在下标*/
   _getProductIndexById: function (id) {
    var data = this.data.cartData,
      len = data.length;
    for (let i = 0; i < len; i++) {
      if (data[i].id == id) {
        return i;
      }
    }
  },

  /*更新购物车商品数据*/
  _resetCartData: function () {
    var newData = this._calcTotalAccountAndCounts(this.data.cartData); /*重新计算总金额和商品总数*/
    this.setData({
      account: newData.account,
      selectedCounts: newData.selectedCounts,
      selectedTypeCounts: newData.selectedTypeCounts,
      cartData: this.data.cartData
    });
  },

  //加减商品
  changeCounts(event){
    var type = cart.getDataSet(event, 'type');
    var id = cart.getDataSet(event, 'id');
    var index = this._getProductIndexById(id);
    var counts = 1;
    if (type == 'add') {
      cart.addCounts(id);
    } else {
      counts = -1;
      cart.cutCounts(id);
    }
    this.data.cartData[index].counts += counts;
    this._resetCartData();
  },
  //删除商品
  delete(event){
    var id = cart.getDataSet(event, 'id');
    var index = this._getProductIndexById(id);
    this.data.cartData.splice(index, 1);//删除某一项商品
    this._resetCartData();
    cart.delete(id);
  },
  //全选
  toggleSelectAll(event){
    var status = cart.getDataSet(event, 'status') == 'true';
    var data = this.data.cartData,
      len = data.length;
    for (let i = 0; i < len; i++) {
      data[i].checkStatus = !status;
    }
    this._resetCartData();
  },
  //下单
  submitOrder(){
    wx.navigateTo({
      url: '../order/order?account='+this.data.account+'&from=cart',
    });
  },

  //授权登录
  login(){
    wx.navigateTo({
      url: '../login/login',
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    cart.execSetStorageSync(this.data.cartData);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})